I created a spatial lines leaflet of London bike rentals. The labels include the name of the street, the number of bikes available for rental, and the number of empty spots. This is a nice dataset for the casual bike renter in London wanting to find out where the best spots are to rent.
First, I initialized the package that this data was found in.
#initialize spData package
library(spData)
## To access larger datasets in this package, install the spDataLarge
## package with: `install.packages('spDataLarge',
## repos='https://nowosad.github.io/drat/', type='source')`
I retrieved the package
#spatial points data for cycle hire points across london
cyclepoints=cycle_hire
Here, I unlisted the latitude and longitude from the geometry column, so I could create vectors from them. I also created a label vector.
#unlist cycle data latitude/longitude
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.3 v purrr 0.3.4
## v tibble 3.1.1 v dplyr 1.0.8
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 2.0.1 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
cyclelatlong <- cyclepoints %>%
mutate(long = unlist(map(cyclepoints$geometry,1)),
lat = unlist(map(cyclepoints$geometry,2)))
#create cycle lat/long/label vectors
latcycle=cyclelatlong$lat
longcycle=cyclelatlong$long
labelcycle=cyclelatlong$name
I initialized leaflet
#initiate leaflet
library(leaflet)
I ran the basic leaflet for London here just to check it
#basic leaflet for london
londonleaflet=leaflet(
options = leafletOptions(
minZoom = 10,
maxZoom = 14,
dragging= TRUE
)
) %>%
addProviderTiles(provider = "Stamen") %>%
setView(lng = -0.1276, lat = 51.5072, zoom=11) %>%
setMaxBounds(
lng1 = -0.1276 +.3,
lng2 = -0.1276 -.3,
lat1 = 51.5072 +.3,
lat2 = 51.5072 -.3
)
londonleaflet
Then, I created labels and put the cycle points into the leaflet.
#create markers
awesome <- makeAwesomeIcon(
icon = "info",
iconColor = "black",
markerColor = "blue",
library = "fa"
)
#long leaflet with cycle markers
cyclemarkers=londonleaflet %>%
addAwesomeMarkers(lng = longcycle,lat = latcycle,label = as.character(labelcycle),
icon = awesome
)
cyclemarkers
Here, I created bin and quantiles for the color scheme for both number of bikes available for hire and number of empty spots.
#create bin/quantiles for number of bikes and number of empty spots
colorBinbikes <- colorBin(
palette = c("red","green","blue"),
domain = cyclelatlong$nbikes,
bins = 3
)
colorQuantilebikes <- colorQuantile(
palette = c("red","orange","green","blue"),
domain = cyclelatlong$nbikes
)
colorQuantileempty <- colorQuantile(
palette = c("red","orange","green","blue"),
domain = cyclelatlong$nempty
)
colorBinempty <- colorBin(
palette = c("red","green","blue"),
domain = cyclelatlong$nempty,
bins = 3
)
Then, I ran the bin for bikes/empty spots. As one might expect, they have an inverse relationship - more bikes, less empty spots and vice versa. We can also see that there are less bikes available in the middle and east of London. Therefore, most people rent bikes from the central heart of London, and also a decent amount on the east end. As we move toward the outer edge of London, the number of bikes available increases, and the number of empty spots decrease. So, bike rentals are lower on the edge of London.
#color bin for number of bikes
leaflet(data=cyclelatlong) %>%
addProviderTiles("Stamen")%>%
addCircleMarkers(
lng = longcycle,
lat = latcycle,
label = paste(cyclelatlong$name,cyclelatlong$nbikes),
color = ~colorBinbikes(cyclelatlong$nbikes)
) %>%
addLegend(pal = colorBinbikes,
values = cyclelatlong$nbikes,
title = "Numer of Bikes")
#color bin for number of empty spaces
leaflet(data=cyclelatlong) %>%
addProviderTiles("Stamen")%>%
addCircleMarkers(
lng = longcycle,
lat = latcycle,
label = paste(cyclelatlong$name,cyclelatlong$nempty),
color = ~colorBinempty(cyclelatlong$nempty)
) %>%
addLegend(pal = colorBinempty,
values = cyclelatlong$nempty,
title = "Numer of Empty Spots")
Finally, I ran the quartile for bikes and empty spots. I reached the same conclusions as I did for the bin.
#quartile bin for number of bikes
leaflet(data=cyclelatlong) %>%
addProviderTiles("Stamen")%>%
addCircleMarkers(
lng = longcycle,
lat = latcycle,
label = paste(cyclelatlong$name,cyclelatlong$nbikes),
color = ~colorQuantilebikes(cyclelatlong$nbikes)
) %>%
addLegend(pal = colorQuantilebikes,
values = cyclelatlong$nbikes,
title = "Quartile of Bikes")
#quartile bin for number of empty spaces
leaflet(data=cyclelatlong) %>%
addProviderTiles("Stamen")%>%
addCircleMarkers(
lng = longcycle,
lat = latcycle,
label = paste(cyclelatlong$name,cyclelatlong$nempty),
color = ~colorQuantileempty(cyclelatlong$nempty)
) %>%
addLegend(pal = colorQuantileempty,
values = cyclelatlong$empty,
title = "Quartile of Empty Spots")